package com.express.common.result;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;
import org.springframework.validation.Errors;

import java.util.Date;

@Data
//@Api(tags = "响应结果")
public class Result<T> {

//    @ApiModelProperty("是否成功")
    private boolean success;

//    @ApiModelProperty("码值")
    private Integer code;

//    @ApiModelProperty("描述")
    @JsonInclude(value = JsonInclude.Include.NON_NULL)
    private String message;

//    @ApiModelProperty("数据")
    private T data;

//    @ApiModelProperty("时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date dateTime;

    private Result() {
    }

    private static <T> Result<T> getMe() {
        return new Result<>();
    }

    private static <T> Result<T> okResult() {
        Result<T> result = getMe();
        result.setSuccess(true);
        result.setDateTime(new Date());
        return result;
    }

    private static <T> Result<T> errorResult() {
        Result<T> result = getMe();
        result.setSuccess(false);
        result.setDateTime(new Date());
        return result;
    }

    public static <T> Result<T> ok() {
        Result<T> result = okResult();
        result.setCode(0);
        return result;
    }

    public static <T> Result<T> ok(Integer resultCode) {
        Result<T> result = okResult();
        result.setCode(resultCode);
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = okResult();
        result.setCode(200);
        result.setData(data);
        return result;
    }
    public static <T> Result<T> ok(T data,Errors bindingResult) {
        Result<T> result = null;
        if(checkParams(bindingResult)){
            result = errorResult();
            result.setCode(201);
            result.setMessage(bindingResult.getAllErrors().get(0).getDefaultMessage());
        } else {
            result = okResult();
            result.setData(data);
            result.setCode(0);
        }
        return result;
    }

    public static <T> Result<T> ok(Integer resultCode, T data) {
        Result<T> result = okResult();
        result.setCode(resultCode);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> ok(T data, String message) {
        Result<T> result = okResult();
        result.setCode(0);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> ok(Integer resultCode, T data, String message) {
        Result<T> result = okResult();
        result.setCode(resultCode);
        result.setData(data);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error(Integer errorCode) {
        Result<T> result = errorResult();
        result.setCode(errorCode);
        return result;
    }

    public static <T> Result<T> error(Integer errorCode, String message) {
        Result<T> result = errorResult();
        result.setCode(errorCode);
        result.setMessage(message);
        return result;
    }

    public static <T> Result<T> error(Integer errorCode, T data) {
        Result<T> result = errorResult();
        result.setCode(errorCode);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> error(Integer errorCode, String message, T data) {
        Result<T> result = errorResult();
        result.setCode(errorCode);
        result.setMessage(message);
        result.setData(data);
        return result;
    }

    public static boolean checkParams(Errors bindingResult) {
        // 验证是否通过
        return bindingResult.hasErrors();
    }

}
